import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * 解析棋谱
 *
 * @author Administrator
 */
public class Parse {

    public Parse() { }

    /**
     * 解析后棋子位置
     * 如：炮二平五（二和五）
     * pawnPosition【第一个值】【第二个值】
     */
    public static int[] pawnPosition = new int[3];


    /**
     * 棋谱读取路径
     * nChess 棋谱编号
     */
    final String GAME_FILE = Game.GAME_FILE;
    int nChess;
    Map<Integer, String> filterMap = new LinkedHashMap<>();

    /**
     * 判断文件是否能被读取
     */
    public void init() {
        try {
            Scanner scanner = new Scanner(new File(GAME_FILE));
            loadPgn(scanner);
        } catch (
                FileNotFoundException e) {
            System.out.println("游戏文件读取错误！");
        }

    }

    /**
     * 解析棋谱
     *
     * @param scanner 扫描结果
     *                返回 棋子代号、棋子移动前的行、列
     *                棋子移动目标的行、列 编号 棋步
     */
    private void loadPgn(Scanner scanner) {
        nChess = scanner.nextInt();
        for (int i = 0; i < nChess; i++) {
            String number = scanner.next();
            String move = scanner.next();

            int numberParam = Integer.parseInt(number.substring(0, number.indexOf(".")));
            filterMap.put(numberParam, move);
        }
    }

    /**
     * 将棋谱首个字解析成象棋代号
     *
     * @param scanner 一条规则（炮二平五）
     * @param number  规则编号
     * @return 棋子代号
     */
    public int wordPars(int number, String scanner) {
        scanner = scanner.substring(0, 1);
        if (number % 2 != 0) {
            //红方
            switch (scanner) {
                case "炮":
                    return 66;
                case "兵":
                    return 77;
                case "车":
                    return 11;
                case "马":
                    return 22;
                case "相":
                    return 33;
                case "仕":
                    return 44;
                case "帅":
                    return 55;
                default:
                    break;
            }
        } else {
            //黑方
            switch (scanner) {
                case "炮":
                    return 6;
                case "卒":
                    return 7;
                case "车":
                    return 1;
                case "马":
                    return 2;
                case "象":
                    return 3;
                case "士":
                    return 4;
                case "将":
                    return 5;
                default:
                    break;
            }
        }
        return -1;
    }

    /**
     * 解析棋谱第二和第四个数
     *
     * @param scanner 一条规则（炮二平五）
     * @return 一维数组【第一个数】【第二个数】
     * isNumber  1是红方 , 0是黑方
     */
    public int[] pawnPars(String scanner) {
        String fistScanner = scanner.substring(1, 2);
        String lastScanner = scanner.substring(3, 4);

        //判断是否为数字
        if (!Character.isDigit(fistScanner.charAt(0))) {
            pawnPosition[0] = quantize(fistScanner) - 1;
            pawnPosition[1] = quantize(lastScanner);
        } else {

            pawnPosition[0] = Integer.parseInt(fistScanner) - 1;
            pawnPosition[1] = Integer.parseInt(lastScanner);
        }

        return pawnPosition;
    }

    /**
     * 将中文数字转成阿拉伯数字
     *
     * @param scanner 单个中文数字字符串
     * @return 阿拉伯数字
     */
    public static int quantize(String scanner) {
        switch (scanner) {
            case "一":
                return 1;
            case "二":
                return 2;
            case "三":
                return 3;
            case "四":
                return 4;
            case "五":
                return 5;
            case "六":
                return 6;
            case "七":
                return 7;
            case "八":
                return 8;
            case "九":
                return 9;
            default:
                break;
        }
        return -1;
    }

    /**
     * 读取规则判断棋子移动方向是是前、退还是平移
     *
     * @param scanner 一条规则（炮二平五）
     * @return 标识
     */
    public String moveDirection(String scanner) {
        scanner = scanner.substring(2, 3);
        switch (scanner) {
            case "进":
                return "into";
            case "退":
                return "back";
            case "平":
                return "parallel";
            default:
                return "无法识别";
        }
    }
}
